<?php

/*
 * @author Dudnex
 * @status TESTED, NOT APPROVED
 */

/* If your DB is empty go to:
 * 1) http://localhost/phpmyadmin/index.php
 * 2) Select databases > Create new DB named "gramasoft" and set Collation to utf8_lithuanian_ci
 * 3) Then go to Import tab and select db_creater.sql
 * It's a temporary solution for importing DB
 */

/**
 * Returns list of support measure code,  measure name and direction from SUPPORT table
 * 
 * @return array[CODE, SUPPORT_MEASURE, DIRECTION_ID][INDEX]
 */
function get_support_measures() {
    $result = mysql_query("SELECT * FROM support");
    $support_measures = array("CODE" => array(), "TITLE" => array(), "DIRECTION_ID" => array());

    print_r(mysql_error());

    $index = 0;
    while ($row = mysql_fetch_array($result)) {
        $support_measures["CODE"][$index] = $row["code"];
        $support_measures["TITLE"][$index] = $row["title"];
        $support_measures["DIRECTION_ID"][$index] = $row["direction_id"];
        $index++;
    }

    mysql_free_result($result);
    return $support_measures;
}

/**
 * Associative array of support measures.
 * 
 * @return array [code]=>title
 */
function get_support_measures_associative() {
    $result = DB::query("SELECT code, title FROM support");
    $rows = array();
    while (list($code, $title) = DB::row($result)) {
        $rows[$code] = $title;
    }
    return $rows;
}

/**
 * Returns list of subdivision codes and title from SUBDIVISIONS table
 * 
 * @return array [CODE, TITLE][INDEX]
 */
function get_subdivisios() {
    $result = mysql_query("SELECT * FROM subdivisions");
    $subdivisions = array("CODE" => array(), "TITLE" => array());

    $index = 0;
    while ($row = mysql_fetch_array($result)) {
        $subdivisions["CODE"][$index] = $row["code"];
        $subdivisions["TITLE"][$index] = $row["title"];
        $index++;
    }

    mysql_free_result($result);
    return $subdivisions;
}

/**
 * Associative array of subdivisions.
 * 
 * @return array [code]=>title
 */
function get_subdivisios_associative() {
    $result = mysql_query("SELECT code, title FROM subdivisions");
    $rows = array();
    while (list($code, $title) = mysql_fetch_array($result)) {
        $rows[$code] = $title;
    }
    return $rows;
}


/**
 * Associative array of support measures.
 * 
 * @return array [code]=>title
 */
function get_subdivisios_measures_associative() {
    $result = mysql_query("SELECT code, title FROM support");
    $rows = array();
    while (list($code, $title) = mysql_fetch_array($result)) {
        $rows[$code] = $title;
    }
    return $rows;
}

/**
 * @return array [subdivision] => array ( Application )
 */
function get_hystorical_data() {
    $result = mysql_query("SELECT date, measure_code, received, administrated FROM historical_quantities ORDER BY measure_code, `date`");
    //FIXME: not subdivisions
    $subdivisions = array();
    $last_subdivision = '';
    while (list($date, $subdivision, $received, $administrated) = mysql_fetch_array($result)) {
        $subdivisions[$subdivision][] = new Application($subdivision, $date, $received, $administrated);
    }
    return $subdivisions;
}

/**
 * Returns list of information system codes and titles from INFORMATION_SYSTEMS table
 * 
 * @return array[CODE, TITLE][INDEX]
 */
function get_information_systems() {
    $result = mysql_query("SELECT * FROM information_systems ORDER BY code");
    $is = array("CODE" => array(), "TITLE" => array());

    $index = 0;
    while ($row = mysql_fetch_array($result)) {
        $is["CODE"][$index] = $row["code"];
        $is["TITLE"][$index] = $row["title"];
        $index++;
    }

    mysql_free_result($result);
    return $is;
}

/**
 * Returns list of is code, subdivision code and date from IS_SUBDIVISIONS table,
 * 
 * @return array[IS_CODE, SUBDIVISION_CODE, DATE][INDEX]
 */
function get_is_subdivisions() {
    $result = mysql_query("SELECT * FROM is_subdivisions");
    $is_subdivisions = array("IS_CODE" => array(), "SUBDIVISION_CODE" => array(), "DATE" => array());

    $index = 0;
    while ($row = mysql_fetch_array($result)) {
        $is_subdivisions["IS_CODE"][$index] = $row["is_code"];
        $is_subdivisions["SUBDIVISION_CODE"][$index] = $row["subdivision_code"];
        $is_subdivisions["DATE"][$index] = $row["date"];
        $index++;
    }

    mysql_free_result($result);
    return $is_subdivisions;
}

/**
 * Associative array of support measures.
 * 
 * @return array [code]=>title
 */
function get_is_associative() {
    $result = DB::query("SELECT code, title FROM information_systems");
    $rows = array();
    while (list($code, $title) = DB::row($result)) {
        $rows[$code] = $title;
    }
    return $rows;
}

/**
 * Associative array of support measures.
 * 
 * @return array [subdivision_cod]=>is_code
 */
function get_subdivision_is() {
    $result = DB::query("SELECT subdivision_code, is_code FROM is_subdivisions ORDER BY subdivision_code");
    $rows = array();
    $last_subdivision = '';
    while (list($subdivision, $is) = DB::row($result)) {
        $rows[$subdivision][$is] = true;
    }
    return $rows;
}



/**
 * Returns list of date, measure code, received and administrated orders from HISTORICAL_QUANTITIES table
 * 
 * @todo Table is empty for now(2011-10-05)
 * 
 * @return array[DATE, MEASURE_CODE, RECEIVED, ADMINISTRATED][INDEX]
 */
function get_historical_quantities() {
    $result = mysql_query("SELECT * FROM historical_quantities");
    $orders = array("DATE" => array(), "MEASURE_CODE" => array(),
        "RECEIVED" => array(), "ADMINISTRATED" => array());

    $index = 0;
    while ($row = mysql_fetch_array($result)) {
        $orders["DATE"][$index] = $row["date"];
        $orders["MEASURE_CODE"][$index] = $row["measure_code"];
        $orders["RECEIVED"][$index] = $row["received"];
        $orders["ADMINISTRATED"][$index] = $row["administrated"];
        $index++;
    }

    mysql_free_result($result);
    return $orders;
}

/**
 * Returns list of measure code, subdivision code, time and date from SUPPORT_ADMINISTRATION table,
 * 
 * @return array[MEASURE_CODE, SUBDIVISION_CODE, TIME, DATE][INDEX]
 */
function get_support_administration() {
    $result = mysql_query("SELECT * FROM support_administration");
    $support_administration = array("MEASURE_CODE" => array(),
        "SUBDIVISION_CODE" => array(), "TIME" => array(), "DATE" => array());

    $index = 0;
    while ($row = mysql_fetch_array($result)) {
        $support_administration["MEASURE_CODE"][$index] = $row["measure_code"];
        $support_administration["SUBDIVISION_CODE"][$index] = $row["subdivision_code"];
        $support_administration["TIME"][$index] = $row["time"];
        $support_administration["DATE"][$index] = $row["date"];

        $index++;
    }

    mysql_free_result($result);
    return $support_administration;
}

/**
 * Returns list of id and title from DIRECTIONS table
 * 
 * @return array[ID, TITLE][INDEX]
 */
function get_directions() {
    $result = mysql_query("SELECT * FROM directions");
    $measure_direction = array("ID" => array(), "TITLE" => array());

    $index = 0;
    while ($row = mysql_fetch_array($result)) {
        $measure_direction["ID"][$index] = $row["id"];
        $measure_direction["TITLE"][$index] = $row["title"];
        $index++;
    }

    mysql_free_result($result);
    return $measure_direction;
}
?>